<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="exception.jsp"
import="java.util.Enumeration"%>
<%
    String method = request.getMethod();
    String name = request.getParameter("field");
    /* 当用户通过浏览器地址栏访问当前页面或请求中的参数field为空时，重定向至input.html */
    if (method.equals("GET") || name == null || name.equals("")) {
        throw new RuntimeException("请求头的referer不是指定值且指定请求参数为null");
    }
%>
<!DOCTYPE html>
<html>
<head>
<title>隐含对象request</title>
<style type="text/css">
table {
  border-collapse: collapse;
}
</style>
</head>
<body>
  <%=request.getHeader("referer") + "<br>"%>
  <%="遍历所有请求参数的值<br>"%>
  <%
      Enumeration<String> names = request.getParameterNames();
      while (names.hasMoreElements()) {
          String element = names.nextElement();
          out.print(element + "：" + request.getParameter(element) + "<br>");
      }
  %>
  当服务器转发用户的请求至另一页面时，服务器可以在转发请求时在请求中设置属性，并在转发的页面得到这些属性<br>
  请注意，请求转发只能在同一域名内实现<br>
  request是实现HttpServletRequest接口的实例<br>
  response是实现HttpServletResponse接口的实例<br>
  服务端在处理用户的请求时，可以在请求中设置属性<br>
  用户看到的此页面是服务器的响应页面<br>
  <%
      out.print("为处理的请求设置属性<br>");
      String attr1 = "beta", val1 = "val";
      request.setAttribute(attr1, val1);
      out.print("为隐含对象session设置属性");
      String attr2 = "cluster", val2 = "galaxy";
      session.setAttribute(attr2, val2);
  %>
  <form action="response.jsp" method="post">
    <input type="text" name="alpha">
    <input type="hidden" name="<%=attr1%>" value="<%=val1%>">
    <input type="hidden" name="<%=attr2%>" value="<%=val2%>">
    <input type="submit" value="提交">
  </form>
  <span style="color: red">
    下表中的“当前请求”指服务器正在处理的请求，此时还未响应用户
  </span>
  <table border="1">
    <tr>
      <th>当前请求</th>
      <th>值</th>
    </tr>
    <tr>
      <td>上下文路径</td>
      <td><%=request.getContextPath()%></td>
    </tr>
    <tr>
      <td>提交方式</td>
      <td><%=request.getMethod()%></td>
    </tr>
    <tr>
      <td>远程地址</td>
      <td><%=request.getRemoteAddr()%></td>
    </tr>
    <tr>
      <td>会话id</td>
      <td><%=request.getRequestedSessionId()%></td>
    </tr>
    <tr>
      <td>URI</td>
      <td><%=request.getRequestURI()%></td>
    </tr>
    <tr>
      <td>会话</td>
      <td><%=request.getSession()%></td>
    </tr>
    <tr>
      <td>使用的语言以及所属国家/地区</td>
      <td><%=request.getLocale()%></td>
    </tr>
  </table>
  <table border="1">
    <tr>
      <th>当前请求头</th>
      <th>值</th>
    </tr>
    <%
        names = request.getHeaderNames();
        while (names.hasMoreElements()) {
            out.print("<tr>");
            name = names.nextElement();
            out.print("<td>" + name + "</td>");
            out.print("<td>" + request.getHeader(name) + "</td>");
            out.print("</tr>");
        }
    %>
  </table>
</body>
</html>
